package server;

import java.net.Socket;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import shared.ChangeCacheInfoMessage;
import shared.ProtocolMessage;

public class CacheChangeInfoSender {
	private List <Socket> connectedClients;
	private List <Socket> snapshotOfConnectedClients;
	Executor executorForSending;
	
	private class netSender implements Runnable{
		private ProtocolMessage pm;
		private Socket destSocket;
		public netSender(ProtocolMessage pm, Socket destSocket){
			this.pm=pm;
			this.destSocket=destSocket;
		}
		@Override
		public void run() {
			// TODO add sending block
			
		}
		
	}
	
	
	public CacheChangeInfoSender(List<Socket> connectedClients) {
		super();
		this.executorForSending=Executors.newCachedThreadPool();
		this.connectedClients = connectedClients;
	}



	public void send(String info){
		snapshotOfConnectedClients=Collections.unmodifiableList(connectedClients);
		Iterator it=snapshotOfConnectedClients.iterator();
		while(it.hasNext()){
			sendInfoChanging((Socket)it.next(),info);
		}
	}



	private void sendInfoChanging(Socket next, String info) {
		ChangeCacheInfoMessage msg=new ChangeCacheInfoMessage(info);
		this.executorForSending.execute(new netSender(msg, next));
		
	}
	
}
